package com.qxbb.report.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qxbb.report.Enum.ChainRoleType;
import com.qxbb.report.Enum.DepositType;
import com.qxbb.report.Enum.LoginUserType;
import com.qxbb.report.bo.ChainOrderSalerBO;
import com.qxbb.report.bo.ChainOrderWeekBO;
import com.qxbb.report.bo.HeadQuartersSalersBO;
import com.qxbb.report.bo.OperatorBO;
import com.qxbb.report.dao.*;
import com.qxbb.report.dto.ChainSalesWeekDTO;
import com.qxbb.report.dto.PerstoreDTO;
import com.qxbb.report.dto.ChainSalesDTO;
import com.qxbb.report.dto.ChainSalesOrderDTO;
import com.qxbb.report.pojo.ChainSalesOrder;
import com.qxbb.report.pojo.ChainUserInfor;
import com.qxbb.report.service.ChainSalesOrderService;
import com.qxbb.report.utils.DateTimeUtil;
import com.qxbb.report.utils.NumberUtil;
import com.qxbb.report.utils.StringUtils;
import com.qxbb.report.vo.ChainSalesOrderVO;
import com.qxbb.report.vo.ChainStoreVO;
import com.qxbb.report.vo.HeadQuartersOrdersVO;
import com.qxbb.report.vo.WeekVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @author: pebble
 * @data 2023/9/29 16:32
 */
@Service
public class ChainSalesOrderServiceImpl extends ServiceImpl<ChainSalesOrderMapper, ChainSalesOrder> implements ChainSalesOrderService {


    @Autowired
    private ChainSalesOrderMapper chainSalesOrderMapper;

    @Autowired
    private ChainUserInforMapper chainUserInforMapper;

    @Autowired
    private ChainVipPrepaidFlowMapper chainVipPrepaidMapper;

    @Autowired
    private ChainStoreGroupElementmMapper chainStoreGroupElementmMapper;

    @Autowired
    private ChainStoreMapper chainStoreMapper;

    @Override
    public ChainSalesOrderVO selectChainSaleOrder(ChainOrderSalerBO chainOrderSalerBO) {
        if (StringUtils.isNotNull(chainOrderSalerBO) && StringUtils.isNotNull(chainOrderSalerBO.getHandledBy())) {
            ChainSalesDTO chainSalesDTO = chainSalesOrderMapper.selectChainSaleOrder(chainOrderSalerBO);
            ChainSalesOrderVO chainSalesOrderVO = insertChainSalesOrderVO(chainOrderSalerBO, chainSalesDTO);
            return chainSalesOrderVO;
        } else { // 经手人是空
            ChainSalesDTO chainSalesDTO = chainSalesOrderMapper.selectNoHandledbyOrder(chainOrderSalerBO);
            ChainSalesOrderVO chainSalesOrderVO = insertChainSalesOrderVO(chainOrderSalerBO, chainSalesDTO);
            return chainSalesOrderVO;
        }

    }


    public ChainSalesOrderVO insertChainSalesOrderVO(ChainOrderSalerBO chainOrderSalerBO, ChainSalesDTO chainSalesDTO) {


        if (StringUtils.isNotNull(chainSalesDTO)) {
            List<PerstoreDTO> perstoreDTO = chainVipPrepaidMapper.selectAmountByDeposit(chainOrderSalerBO.getChainStore(), chainOrderSalerBO.getStartTime(), chainOrderSalerBO.getEndTime());
            Double cash = 0.0;
            Double card = 0.0;
            Double weCate = 0.0;
            Double alipay = 0.0;
            for (PerstoreDTO dto : perstoreDTO) {
                if (dto.getType().equals(DepositType.CASH.getType()) && StringUtils.isNotNull(dto.getType())) {
                    cash = dto.getPrestoreAmount();
                } else if (dto.getType().equals(DepositType.CARD.getType()) && StringUtils.isNotNull(dto.getType())) {
                    card = dto.getPrestoreAmount();
                } else if (dto.getType().equals(DepositType.WE_CHAT.getType()) && StringUtils.isNotNull(dto.getType())) {
                    weCate = dto.getPrestoreAmount();
                } else if (dto.getType().equals(DepositType.ALIPAY.getType()) && StringUtils.isNotNull(dto.getType())) {
                    alipay = dto.getPrestoreAmount();
                }
            }
            ChainSalesOrderVO chainSalesOrderVO = ChainSalesOrderVO.builder()
                    .totalQuantity(chainSalesDTO.getTotalQuantity())
                    .totalQuantityR(chainSalesDTO.getTotalQuantityR())
                    .netSales(chainSalesDTO.getTotalQuantity() - chainSalesDTO.getTotalQuantityR())
                    .totalQuantityF(chainSalesDTO.getTotalQuantityF())
                    .netAmount(chainSalesDTO.getNetAmount())
                    .netAmountR(chainSalesDTO.getNetAmountR())
                    .inNetAmount(chainSalesDTO.getNetAmount() - chainSalesDTO.getNetAmountR())  // 净销售额
                    .totalDiscountAmount(chainSalesDTO.getTotalDiscountAmount())
                    .netSalesProfit(chainSalesDTO.getNetAmount() - chainSalesDTO.getNetAmountR() - chainSalesDTO.getTotalDiscountAmount() - chainSalesDTO.getNetCostSales() - chainSalesDTO.getTotalCostFree())
                    .coupon(chainSalesDTO.getCoupon())
                    .vipScore(chainSalesDTO.getVipScore())
                    .chainPrepaidAmount(chainSalesDTO.getChainPrepaidAmount())
                    .cardAmount(chainSalesDTO.getCardAmount())
                    .cashAmount(chainSalesDTO.getCashAmount())
                    .weChatAmount(chainSalesDTO.getWeChatAmount())
                    .alipayAmount(chainSalesDTO.getAlipayAmount())
                    .prestoreCash(cash == null ? 0 : cash)
                    .prestoreCard(card == null ? 0 : card)
                    .prestoreWeChat(weCate == null ? 0 : weCate)
                    .prestoreAlipay(alipay == null ? 0 : alipay)
                    .netReceipts((int) (chainSalesDTO.getChainPrepaidAmount() + chainSalesDTO.getCardAmount() + chainSalesDTO.getCashAmount() + chainSalesDTO.getWeChatAmount() + chainSalesDTO.getAlipayAmount()))
                    .build();
            return chainSalesOrderVO;
        }
        return null;
    }


    @Override
    public List<HeadQuartersOrdersVO> selectHeadQuartersOrder(HeadQuartersSalersBO headQuartersSalersBO) {
        List<ChainSalesOrderDTO> chainSalesOrderDTO = chainSalesOrderMapper.selectHeadQuartersOrder(headQuartersSalersBO);
        List<HeadQuartersOrdersVO> list = new ArrayList<>();
        //遍历所有店铺
        for (ChainSalesOrderDTO salesOrderDTO : chainSalesOrderDTO) {
            Integer netSales = salesOrderDTO.getTotalQuantity() - salesOrderDTO.getTotalQuantityR();
            Double cashAmount = salesOrderDTO.getCardAmount() + salesOrderDTO.getCashAmount() + salesOrderDTO.getWeChatAmount() + salesOrderDTO.getAlipayAmount() - salesOrderDTO.getReturnAmount(); // 减去找零
            if (StringUtils.isNull(salesOrderDTO.getTotalDiscountAmount())) {
                salesOrderDTO.setTotalDiscountAmount(0.0);
            }
            if (StringUtils.isNull(salesOrderDTO.getNetCostSales())) {
                salesOrderDTO.setNetCostSales(0.0);
            }
            Double netSalesProfit = salesOrderDTO.getNetAmount() - salesOrderDTO.getNetAmountR() - salesOrderDTO.getTotalCost() - salesOrderDTO.getTotalDiscountAmount() - salesOrderDTO.getTotalCostFree();
            double percentage = 0.0;

            double netAmountCalculated = salesOrderDTO.getNetAmount() - salesOrderDTO.getNetAmountR();
            if (netAmountCalculated != 0) {
                percentage = salesOrderDTO.getQxAmount() / netAmountCalculated;
            }
            HeadQuartersOrdersVO headQuartersOrdersVO = HeadQuartersOrdersVO.builder()
                    .chainName(salesOrderDTO.getChainName())
                    .netSales(netSales)
                    .netAmount(salesOrderDTO.getNetAmount() - salesOrderDTO.getNetAmountR())
                    .qxNetAmount(salesOrderDTO.getQxAmount())
                    .percentage(NumberUtil.df.format(percentage))
                    .cashAmount(cashAmount) // 现金
                    .chainPrepaidAmount(salesOrderDTO.getChainPrepaidAmount())
                    .totalCost(salesOrderDTO.getTotalCost())
                    .netSalesProfit(netSalesProfit)
                    .build();
            list.add(headQuartersOrdersVO);
        }
        return list.stream().sorted(Comparator.comparing(HeadQuartersOrdersVO::getNetAmount).reversed()).collect(Collectors.toList());

    }

    @Override
    public List<HeadQuartersOrdersVO> selectOperatorAll(OperatorBO operatorBO) {
        List<HeadQuartersOrdersVO> vos = new ArrayList<>();
        if (StringUtils.isNotNull(operatorBO) && StringUtils.isNull(operatorBO.getChainStore())) {
            // todo (原判断直营连锁店：位置1，共6处）
            List<Integer> list = chainStoreGroupElementmMapper.selectAllChain(operatorBO.getChainUsreId());
            if (StringUtils.isEmpty(list)){
                ChainUserInfor chainUserInfor = chainUserInforMapper.selectByUserId(operatorBO.getChainUsreId());  // 查询连锁店用户
                list.add(chainUserInfor.getChainId());
            }
            List<ChainSalesOrderDTO> chainSalesOrderDTOS = chainSalesOrderMapper.selectOperatorAll(operatorBO.getStartTime(), operatorBO.getEndTime(), list);
            //遍历所有店铺
            for (ChainSalesOrderDTO salesOrderDTO : chainSalesOrderDTOS) {
                Integer netSales = salesOrderDTO.getTotalQuantity() - salesOrderDTO.getTotalQuantityR();
                Double cashAmount = salesOrderDTO.getCardAmount() + salesOrderDTO.getCashAmount() + salesOrderDTO.getWeChatAmount() + salesOrderDTO.getAlipayAmount() - salesOrderDTO.getReturnAmount();
                if (StringUtils.isNull(salesOrderDTO.getTotalDiscountAmount())) {
                    salesOrderDTO.setTotalDiscountAmount(0.0);
                }
                if (StringUtils.isNull(salesOrderDTO.getNetCostSales())) {
                    salesOrderDTO.setNetCostSales(0.0);
                }
                Double netSalesProfit = salesOrderDTO.getNetAmount() - salesOrderDTO.getNetAmountR() - salesOrderDTO.getTotalDiscountAmount() - salesOrderDTO.getTotalCost() - salesOrderDTO.getTotalCostFree();
                double percentage = 0.0;

                double netAmountCalaculated = salesOrderDTO.getNetAmount() - salesOrderDTO.getNetAmountR();
                if (netAmountCalaculated != 0) {
                    percentage = salesOrderDTO.getQxAmount() / netAmountCalaculated;
                }

                HeadQuartersOrdersVO headQuartersOrdersVO = HeadQuartersOrdersVO.builder()
                        .chainName(salesOrderDTO.getChainName())
                        .netSales(netSales)
                        .netAmount(salesOrderDTO.getNetAmount() - salesOrderDTO.getNetAmountR())
                        .qxNetAmount(salesOrderDTO.getQxAmount())
                        .percentage(NumberUtil.df.format(percentage))
                        .cashAmount(cashAmount)
                        .chainPrepaidAmount(salesOrderDTO.getChainPrepaidAmount())
                        .totalCost(salesOrderDTO.getTotalCost())
                        .netSalesProfit(netSalesProfit)
                        .build();
                vos.add(headQuartersOrdersVO);
            }
            return vos.stream().sorted(Comparator.comparing(HeadQuartersOrdersVO::getNetAmount).reversed()).collect(Collectors.toList());

        } else {

            //单个店铺
            return getCommon(operatorBO);
        }

    }


    public List<HeadQuartersOrdersVO> getCommon(OperatorBO operatorBO) {
        List<HeadQuartersOrdersVO> vos = new ArrayList<>();
        ChainSalesOrderDTO chainSalesOrderDTO = chainSalesOrderMapper.selectOperatorOne(operatorBO.getStartTime(), operatorBO.getEndTime(), operatorBO.getChainStore());
        if (StringUtils.isNotNull(chainSalesOrderDTO)) {
            Integer netSales = chainSalesOrderDTO.getTotalQuantity() - chainSalesOrderDTO.getTotalQuantityR();
            Double cashAmount = chainSalesOrderDTO.getCardAmount() + chainSalesOrderDTO.getCashAmount() + chainSalesOrderDTO.getWeChatAmount() + chainSalesOrderDTO.getAlipayAmount() - chainSalesOrderDTO.getReturnAmount();
            if (StringUtils.isNull(chainSalesOrderDTO.getTotalDiscountAmount())) {
                chainSalesOrderDTO.setTotalDiscountAmount(0.0);
            }
            if (StringUtils.isNull(chainSalesOrderDTO.getNetCostSales())) {
                chainSalesOrderDTO.setNetCostSales(0.0);
            }
            Double netSalesProfit = chainSalesOrderDTO.getNetAmount() - chainSalesOrderDTO.getNetAmountR() - chainSalesOrderDTO.getTotalDiscountAmount() - chainSalesOrderDTO.getTotalCost() - chainSalesOrderDTO.getTotalCostFree();
            Double percentage = 0.0;
            if (chainSalesOrderDTO.getNetAmount() != 0) {
                percentage = Double.parseDouble(String.format("%.2f", chainSalesOrderDTO.getQxAmount() / (chainSalesOrderDTO.getNetAmount() - chainSalesOrderDTO.getNetAmountR()))) * 100;
            }
            HeadQuartersOrdersVO headQuartersOrdersVO = HeadQuartersOrdersVO.builder()
                    .chainName(chainSalesOrderDTO.getChainName())
                    .netSales(netSales)
                    .netAmount(chainSalesOrderDTO.getNetAmount() - chainSalesOrderDTO.getNetAmountR())
                    .qxNetAmount(chainSalesOrderDTO.getQxAmount())
                    .percentage(percentage.toString() + "%")
                    .cashAmount(cashAmount)
                    .chainPrepaidAmount(chainSalesOrderDTO.getChainPrepaidAmount())
                    .totalCost(chainSalesOrderDTO.getTotalCost())
                    .netSalesProfit(netSalesProfit)
                    .build();
            vos.add(headQuartersOrdersVO);

        }
        return vos.stream().sorted(Comparator.comparing(HeadQuartersOrdersVO::getNetSales)).collect(Collectors.toList());
    }


    @Override
    public HashMap<String, List<List<Integer>>> selectChainOrderWeek(ChainOrderWeekBO chainOrderWeekBO) throws ParseException {
        HashMap<String, List<List<Integer>>> hashMap = new HashMap<>();

        List<List<Integer>> timeList = new ArrayList<>();
        List<List<Integer>> netAmountList = new ArrayList<>();

        List<Integer> oneNetAmount = new ArrayList();
        List<Integer> twoNetAmount = new ArrayList<>();
        List<Integer> threeNetAmount = new ArrayList<>();
        List<Integer> fourNetAmount = new ArrayList<>();
        List<Integer> fiveNetAmount = new ArrayList<>();
        List<Integer> sixNetAmount = new ArrayList<>();

        Integer time = Integer.parseInt(DateTimeUtil.getLastDayByMonth(chainOrderWeekBO.getYear(), chainOrderWeekBO.getMonth()).substring(8, 10)); // 当月天数
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.parse(DateTimeUtil.getFirstDayByMonth(chainOrderWeekBO.getYear(), chainOrderWeekBO.getMonth()).substring(0, 10));
        String week = getDayOfTheWeek(date);  // 当月第一天为周几
        Integer one = null;
        Integer two = null;
        Integer three = null;
        Integer four = null;
        Integer five = null;

        List<Integer> listOne = new ArrayList<>();
        List<Integer> listTwo = new ArrayList<>();
        List<Integer> listThree = new ArrayList<>();
        List<Integer> listFour = new ArrayList<>();
        List<Integer> listFive = new ArrayList<>();
        List<Integer> listSix = new ArrayList<>();

        Map<String, Integer> map = getNetAmount(chainOrderWeekBO);

        switch (week) {
            case "星期一":
                listOne.add(1); // 当天时间
                oneNetAmount.add(map.containsKey("1") ? map.get("1") : 0); // 当天净售额
                listOne.add(2);
                oneNetAmount.add(map.containsKey("2") ? map.get("2") : 0);
                listOne.add(3);
                oneNetAmount.add(map.containsKey("3") ? map.get("3") : 0);
                listOne.add(4);
                oneNetAmount.add(map.containsKey("4") ? map.get("4") : 0);
                listOne.add(5);
                oneNetAmount.add(map.containsKey("5") ? map.get("5") : 0);
                listOne.add(6);
                oneNetAmount.add(map.containsKey("6") ? map.get("6") : 0);
                listOne.add(7);
                oneNetAmount.add(map.containsKey("7") ? map.get("7") : 0);
                listOne.add(null);
                one = 7;
                break;
            case "星期二":
                listOne.add(null);
                oneNetAmount.add(null);
                listOne.add(1);
                oneNetAmount.add(map.containsKey("1") ? map.get("1") : 0);
                listOne.add(2);
                oneNetAmount.add(map.containsKey("2") ? map.get("2") : 0);
                listOne.add(3);
                oneNetAmount.add(map.containsKey("3") ? map.get("3") : 0);
                listOne.add(4);
                oneNetAmount.add(map.containsKey("4") ? map.get("4") : 0);
                listOne.add(5);
                oneNetAmount.add(map.containsKey("5") ? map.get("5") : 0);
                listOne.add(6);
                oneNetAmount.add(map.containsKey("6") ? map.get("6") : 0);
                listOne.add(null);
                one = 6;
                break;
            case "星期三":
                listOne.add(null);
                oneNetAmount.add(null);
                listOne.add(null);
                oneNetAmount.add(null);
                listOne.add(1);
                oneNetAmount.add(map.containsKey("1") ? map.get("1") : 0);
                listOne.add(2);
                oneNetAmount.add(map.containsKey("2") ? map.get("2") : 0);
                listOne.add(3);
                oneNetAmount.add(map.containsKey("3") ? map.get("3") : 0);
                listOne.add(4);
                oneNetAmount.add(map.containsKey("4") ? map.get("4") : 0);
                listOne.add(5);
                oneNetAmount.add(map.containsKey("5") ? map.get("5") : 0);
                listOne.add(null);
                one = 5;
                break;
            case "星期四":
                listOne.add(null);
                oneNetAmount.add(null);
                listOne.add(null);
                oneNetAmount.add(null);
                listOne.add(null);
                oneNetAmount.add(null);
                listOne.add(1);
                oneNetAmount.add(map.containsKey("1") ? map.get("1") : 0);
                listOne.add(2);
                oneNetAmount.add(map.containsKey("2") ? map.get("2") : 0);
                listOne.add(3);
                oneNetAmount.add(map.containsKey("3") ? map.get("3") : 0);
                listOne.add(4);
                oneNetAmount.add(map.containsKey("4") ? map.get("4") : 0);
                listOne.add(null);
                one = 4;
                break;
            case "星期五":
                listOne.add(null);
                oneNetAmount.add(null);
                listOne.add(null);
                oneNetAmount.add(null);
                listOne.add(null);
                oneNetAmount.add(null);
                listOne.add(null);
                oneNetAmount.add(null);
                listOne.add(1);
                oneNetAmount.add(map.containsKey("1") ? map.get("1") : 0);
                listOne.add(2);
                oneNetAmount.add(map.containsKey("2") ? map.get("2") : 0);
                listOne.add(3);
                oneNetAmount.add(map.containsKey("3") ? map.get("3") : 0);
                listOne.add(null);
                one = 3;
                break;
            case "星期六":
                listOne.add(null);
                oneNetAmount.add(null);
                listOne.add(null);
                oneNetAmount.add(null);
                listOne.add(null);
                oneNetAmount.add(null);
                listOne.add(null);
                oneNetAmount.add(null);
                listOne.add(null);
                oneNetAmount.add(null);
                listOne.add(1);
                oneNetAmount.add(map.containsKey("1") ? map.get("1") : 0);
                listOne.add(2);
                oneNetAmount.add(map.containsKey("2") ? map.get("2") : 0);
                listOne.add(null);
                one = 2;
                break;
            case "星期日":
                listOne.add(null);
                oneNetAmount.add(null);
                listOne.add(null);
                oneNetAmount.add(null);
                listOne.add(null);
                oneNetAmount.add(null);
                listOne.add(null);
                oneNetAmount.add(null);
                listOne.add(null);
                oneNetAmount.add(null);
                listOne.add(null);
                oneNetAmount.add(null);
                listOne.add(1);
                oneNetAmount.add(map.containsKey("1") ? map.get("1") : 0);
                listOne.add(null);
                one = 1;
                break;
            default:
                break;

        }

        // 第二行日期和数据
        for (int i = one + 1; i < one + 8; i++) {
            if (i < time) {
                String index = String.valueOf(i);
                listTwo.add(i);
                twoNetAmount.add(map.containsKey(index) ? map.get(index) : 0);
            }
            two = one + 8;
        }
        listTwo.add(null);

        // 第三行日期和数据
        for (int i = two; i < two + 7; i++) {
            if (i < time) {
                String index = String.valueOf(i);
                listThree.add(i);
                threeNetAmount.add(map.containsKey(index) ? map.get(index) : 0);
            }
            three = two + 7;
        }
        listThree.add(null);
        // 第四行日期和数据
        for (int i = three; i < three + 7; i++) {
            if (i < time) {
                String index = String.valueOf(i);
                listFour.add(i);
                fourNetAmount.add(map.containsKey(index) ? map.get(index) : 0);
            }
            four = three + 7;
        }
        listFour.add(null);
        // 第五行日期和数据
        for (int i = four; i < four + 7; i++) {
            if (i <= time) {
                String index = String.valueOf(i);
                listFive.add(i);
                fiveNetAmount.add(map.containsKey(index) ? map.get(index) : 0);
            } else {
                listFive.add(null);
                fiveNetAmount.add(null);
            }
            five = four + 7;
        }
        listFive.add(null);
        // 第六行日期和数据
        for (int i = five; i < five + 7; i++) {
            if (i <= time) {
                String index = String.valueOf(i);
                listSix.add(i);
                sixNetAmount.add(map.containsKey(index) ? map.get(index) : 0);
            } else {
                listSix.add(null);
                sixNetAmount.add(null);
            }
        }
        listSix.add(null);
        timeList.add(listOne);
        timeList.add(listTwo);
        timeList.add(listThree);
        timeList.add(listFour);
        timeList.add(listFive);
        timeList.add(listSix);


        // 每周合计金额统计
        Integer sumOne = 0;
        for (Integer i : oneNetAmount) {
            if (i != null) {
                sumOne += i;
            }
        }
        oneNetAmount.add(sumOne);

        Integer sumTwo = 0;
        for (Integer i : twoNetAmount) {
            sumTwo += i;
        }
        twoNetAmount.add(sumTwo);

        Integer sumThree = 0;
        for (Integer i : threeNetAmount) {
            sumThree += i;
        }
        threeNetAmount.add(sumThree);

        Integer sumFour = 0;
        for (Integer i : fourNetAmount) {
            sumFour += i;
        }
        fourNetAmount.add(sumFour);

        Integer sumFive = 0;
        for (Integer i : fiveNetAmount) {
            if (i != null) {
                sumFive += i;
            }
        }
        fiveNetAmount.add(sumFive);

        Integer sumSix = 0;
        for (Integer i : sixNetAmount) {
            if (i != null) {
                sumSix += i;
            }

        }
        sixNetAmount.add(sumSix);

        netAmountList.add(oneNetAmount);
        netAmountList.add(twoNetAmount);
        netAmountList.add(threeNetAmount);
        netAmountList.add(fourNetAmount);
        netAmountList.add(fiveNetAmount);
        netAmountList.add(sixNetAmount);
        hashMap.put("time", timeList);
        hashMap.put("netAmount", netAmountList);
        return hashMap;
    }

    @Override
    public List<List<Integer>> selectMonth(ChainOrderWeekBO chainOrderWeekBO) throws ParseException {
        List<List<Integer>> timeList = new ArrayList<>();

        Integer time = Integer.parseInt(DateTimeUtil.getLastDayByMonth(chainOrderWeekBO.getYear(), chainOrderWeekBO.getMonth()).substring(8, 10)); // 当月天数
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.parse(DateTimeUtil.getFirstDayByMonth(chainOrderWeekBO.getYear(), chainOrderWeekBO.getMonth()).substring(0, 10));
        String week = getDayOfTheWeek(date);  // 当月第一天为周几
        Integer one = null;
        Integer two = null;
        Integer three = null;
        Integer four = null;
        Integer five = null;

        List<Integer> listOne = new ArrayList<>();
        List<Integer> listTwo = new ArrayList<>();
        List<Integer> listThree = new ArrayList<>();
        List<Integer> listFour = new ArrayList<>();
        List<Integer> listFive = new ArrayList<>();
        List<Integer> listSix = new ArrayList<>();

        switch (week) {
            case "星期一":
                listOne.add(1); // 当天时间
                listOne.add(2);
                listOne.add(3);
                listOne.add(4);
                listOne.add(5);
                listOne.add(6);
                listOne.add(7);
                listOne.add(null);
                one = 7;
                break;
            case "星期二":
                listOne.add(null);
                listOne.add(1);
                listOne.add(2);
                listOne.add(3);
                listOne.add(4);
                listOne.add(5);
                listOne.add(6);
                listOne.add(null);
                one = 6;
                break;
            case "星期三":
                listOne.add(null);
                listOne.add(null);
                listOne.add(1);
                listOne.add(2);
                listOne.add(3);
                listOne.add(4);
                listOne.add(5);
                listOne.add(null);
                one = 5;
                break;
            case "星期四":
                listOne.add(null);
                listOne.add(null);
                listOne.add(null);
                listOne.add(1);
                listOne.add(2);
                listOne.add(3);
                listOne.add(4);
                listOne.add(null);
                one = 4;
                break;
            case "星期五":
                listOne.add(null);
                listOne.add(null);
                listOne.add(null);
                listOne.add(null);
                listOne.add(1);
                listOne.add(2);
                listOne.add(3);
                listOne.add(null);
                one = 3;
                break;
            case "星期六":
                listOne.add(null);
                listOne.add(null);
                listOne.add(null);
                listOne.add(null);
                listOne.add(null);
                listOne.add(1);
                listOne.add(2);
                listOne.add(null);
                one = 2;
                break;
            case "星期日":
                listOne.add(null);
                listOne.add(null);
                listOne.add(null);
                listOne.add(null);
                listOne.add(null);
                listOne.add(null);
                listOne.add(1);
                listOne.add(null);
                one = 1;
                break;
            default:
                break;

        }

        // 第二行日期和数据
        for (int i = one + 1; i < one + 8; i++) {
            if (i < time) {
                listTwo.add(i);
            }
            two = one + 8;
        }
        listTwo.add(null);

        // 第三行日期和数据
        for (int i = two; i < two + 7; i++) {
            if (i < time) {
                listThree.add(i);
            }
            three = two + 7;
        }
        listThree.add(null);
        // 第四行日期和数据
        for (int i = three; i < three + 7; i++) {
            if (i < time) {
                listFour.add(i);
            }
            four = three + 7;
        }
        listFour.add(null);
        // 第五行日期和数据
        for (int i = four; i < four + 7; i++) {
            if (i <= time) {
                listFive.add(i);
            } else {
                listFive.add(null);
            }
            five = four + 7;
        }
        listFive.add(null);
        // 第六行日期和数据
        for (int i = five; i < five + 7; i++) {
            if (i <= time) {
                listSix.add(i);
            } else {
                listSix.add(null);
            }
        }
        listSix.add(null);
        timeList.add(listOne);
        timeList.add(listTwo);
        timeList.add(listThree);
        timeList.add(listFour);
        timeList.add(listFive);
        timeList.add(listSix);
        return timeList;
    }

    //根据日期取得星期几
    public static String getDayOfTheWeek(Date date) {
        String[] weeks = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return weeks[week_index];
    }

    public Map<String, Integer> getNetAmount(ChainOrderWeekBO chainOrderWeekBO) {
        Map<String, Integer> map = new HashMap<>();
        List<Integer> storeList = new ArrayList<>();
        String startTime = DateTimeUtil.getFirstDayByMonth(chainOrderWeekBO.getYear(), chainOrderWeekBO.getMonth()).substring(0, 10);
        String endTime = DateTimeUtil.getLastDayByMonth(chainOrderWeekBO.getYear(), chainOrderWeekBO.getMonth()).substring(0, 10);
        // 判断店铺信息是否为空
        if (StringUtils.isNull(chainOrderWeekBO.getChainStore())) {
            // 判断用户类型
            if (chainOrderWeekBO.getUserType() == LoginUserType.HEADQUARTERS.getNumber()) { // 总部用户
                List<Integer> chainIds = chainStoreMapper.getAllParentStore(); // 所有店铺(parent为null店铺）
                map = getToDayNetAmount(chainIds, startTime, endTime);
            } else if (chainOrderWeekBO.getUserType() == LoginUserType.chain_store.getNumber()) {// 连锁店用户
                ChainUserInfor chainUserInfor = chainUserInforMapper.selectByUserId(chainOrderWeekBO.getUserId()); // 连锁店用户信息
                // 判断是否是连锁店经营者
                if (chainUserInfor.getRoleType() == ChainRoleType.CHAIN_STORE_OPERATOR.getRoleType()) {
                    // todo 直营连锁店信息：位置2 ，共6处
                    //连锁店经营者
                    List<Integer> storeIds = chainStoreGroupElementmMapper.selectChainParentIsNull(chainOrderWeekBO.getUserId());
                    if (StringUtils.isEmpty(storeIds)){
                        ChainStoreVO chainStoreVO = chainStoreMapper.selectChainId(chainOrderWeekBO.getUserId());
                        storeIds.add(chainStoreVO.getValue());
                    }
                    map = getToDayNetAmount(storeIds, startTime, endTime);

                } else {  // 普通店员 --》单店铺月统计销售
                    storeList.add(chainOrderWeekBO.getChainStore());
                    map = getToDayNetAmount(storeList, startTime, endTime);
                }
            }
        } else {  // 店铺信息不为空 --》单店铺月统计销售
            storeList.add(chainOrderWeekBO.getChainStore());
            map = getToDayNetAmount(storeList, startTime, endTime);
        }
        return map;
    }


    public Map<String, Integer> getToDayNetAmount(List<Integer> chainIds, String startTime, String endTime) {
        Map<String, Integer> map = new HashMap<>();
        List<ChainSalesWeekDTO> list = chainSalesOrderMapper.selectAllNetAmount(chainIds, startTime, endTime);
        for (ChainSalesWeekDTO chainSalesWeekDTO : list) {
            if (chainSalesWeekDTO.getTime().charAt(8) == '0') {
                chainSalesWeekDTO.setTime(chainSalesWeekDTO.getTime().substring(9, 10));
            } else {
                chainSalesWeekDTO.setTime(chainSalesWeekDTO.getTime().substring(8, 10));
            }
        }
//        map = list.stream().collect(Collectors.toMap(ChainSalesWeekDTO::getTime, x -> x));
        map = list.stream().collect(Collectors.toMap(ChainSalesWeekDTO::getTime, ChainSalesWeekDTO::getNetAmount, (value1, value2) -> value1));
        return map;
    }

}
